Skip to content

fix: use del instead of explicit shutdown in CombinedLoader.reset()#21708

Merged
tchaton merged 3 commits into
Lightning-AI:masterfrom
michaelvay:bugfix/21703_combined_loader_reset
Jun 1, 2026
Merged

fix: use del instead of explicit shutdown in CombinedLoader.reset()#21708
tchaton merged 3 commits into
Lightning-AI:masterfrom
michaelvay:bugfix/21703_combined_loader_reset

Conversation

@michaelvay
Copy link
Copy Markdown
Contributor

@michaelvay michaelvay commented May 10, 2026

What does this PR do?

The PR replaces the usages of explicit usage of dataloadet._iterator._shutdown_wokers() with del dataloadet._iterator.

This is the expected way to shutdown mutliprocessing iterator in pytorch:
https://github.com/pytorch/pytorch/blob/main/torch/utils/data/dataloader.py#L1685
They also mention that del should be the way to shutdown in the _MultiProcessingDataLoaderIter shutdown design note:
https://github.com/pytorch/pytorch/blob/main/torch/utils/data/dataloader.py#L787

Since errors may occur during shutdown_workers lightning is expected to mask them as pytorch does, by calling _shutdown_workers in the context of __del__ which suppresses errors (https://docs.python.org/3/reference/datamodel.html#object.__del__)

calling _shutdown_workers() explicitly raises any exceptions raised by the shutdown process and results in exit code 1, which defers from pytorch behavior where same reproduce script will exit with exit code 0.

test_loops had to be changed since asserted shutdown_workers called twice with persistent workers once in teardown and once in del (as expected), in the new implementation its called once in del

Fixes #21703

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--21708.org.readthedocs.build/en/21708/

@github-actions github-actions Bot added the pl Generic label for PyTorch Lightning package label May 10, 2026
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 10, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79%. Comparing base (932b7e3) to head (9d33540).
✅ All tests successful. No failed tests found.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

❗ There is a different number of reports uploaded between BASE (932b7e3) and HEAD (9d33540). Click for more details.

HEAD has 907 uploads less than BASE
Flag BASE (932b7e3) HEAD (9d33540)
cpu 248 42
python 18 3
lightning_fabric 80 0
pytest 123 0
python3.12 71 12
python3.13 53 9
lightning 88 15
python3.11 35 6
python3.12.7 53 9
python3.10 18 3
pytorch2.5.1 9 3
pytest-full 125 42
pytorch2.8 18 6
pytorch_lightning 80 27
pytorch2.9 18 6
pytorch2.10 18 6
pytorch2.1 18 6
pytorch2.2.2 9 3
pytorch2.3 9 3
pytorch2.7 8 3
pytorch2.6 9 3
pytorch2.4.1 9 3
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #21708     +/-   ##
=========================================
- Coverage      87%      79%     -8%     
=========================================
  Files         270      267      -3     
  Lines       23973    23914     -59     
=========================================
- Hits        20748    18801   -1947     
- Misses       3225     5113   +1888     

@michaelvay michaelvay force-pushed the bugfix/21703_combined_loader_reset branch from 2ef830b to b4aba16 Compare May 10, 2026 14:47
@michaelvay michaelvay force-pushed the bugfix/21703_combined_loader_reset branch from b4aba16 to 4e02938 Compare May 11, 2026 10:50
@michaelvay
Copy link
Copy Markdown
Contributor Author

Hi @deependujha, thanks for the green light on this, let me know if anything is missing 🙏

Comment thread src/lightning/pytorch/CHANGELOG.md Outdated
@michaelvay
Copy link
Copy Markdown
Contributor Author

Hi @deependujha, is there anything missing from this PR in order for a merge?
Does the CI errors is something I can handle on my side? is it required for the merge? looks like these are permissions error for the bot.

@tchaton tchaton merged commit 1bd737a into Lightning-AI:master Jun 1, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pl Generic label for PyTorch Lightning package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: explicit dataloader iterator _shutdown_workers() call causes lightning to crash when pytorch ignores the error

4 participants